<template>
  <div class="inventoryPosting">
    <div class="header">
      <div class="header-name">
        <search
          :options="selectData"
          :search-son="searchSon"
          :msg="tacitly"
          @seek="collect"
          @hand="handleQuerySonClass"
        />
        <el-button type="primary" class="btn" size="mini" @click="handleQuerySonClass">{{ $t('btn.search') }}</el-button>
        <el-button type="primary" size="mini" @click="handleExport">{{ $t('btn.toExcel') }}</el-button>
        <!--        <filtration :ftype="ftype" :msg="tacitly" :select-data="selectData" :table-header="tableHeader1" @seachData="handleQuerySonClass" />-->
      </div>
    </div>
    <div class="table-content">
      <jc-table
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        serial
        sortable
        get-row-keys="uuid"
        :get-summaries="getSummaries"
        show-summary
        lazy
        :load="load"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <template v-slot:tagSlot="col">
          <el-link type="primary" @click="jumpMateriel(col.scope.row)">{{ col.scope.row.fnumber }}</el-link>
        </template>
      </jc-table>
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        v-show="total > 0"
        :total="total"
        :page.sync="currentPage"
        :limit.sync="size"
        @pagination="handleGetUntreated"
      />
    </div>
  </div>
</template>

<script>
import {
  exportSalOrderCostList,
  querySalDedailCostList,
  querySalOrderCostList
} from '@/api/financial/salesMargin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import searData from '@/components/Search/mixin'
import search from '@/components/Search'
export default {
  name: 'OtherDetail',
  components: {
    search
  },
  mixins: [searData],
  data() {
    return {
      ftype: 83,
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      tableHeight: '73vh',
      // 表头
      tableData: [], // 销售数据
      tableHeader: [
        { label: this.$t('market.orderDate'), prop: 'fdate' },
        { label: this.$t('market.billNo'), prop: 'fbillno', width: '110' },
        { label: this.$t('market.billType'), prop: 'fbilltypename' },
        { label: this.$t('market.orderfseq'), prop: 'fseq' },
        { label: this.$t('shop.fmodel'), prop: 'fmodel' },
        { label: this.$t('shop.fnumber1'), prop: 'fnumber', type: 'tag', width: '110' },
        { label: this.$t('shop.fname2'), prop: 'fname' },
        { label: this.$t('shop.fspecificaTion'), prop: 'fspecification' },
        { label: this.$t('market.materialGroup'), prop: 'fmaterialgroupname' },
        { label: this.$t('market.frowType'), prop: 'frowtypename' },
        { label: this.$t('market.category'), prop: 'fcategoryname' },
        { label: this.$t('market.saleUnit'), prop: 'funit' },
        { label: this.$t('market.fqty2'), prop: 'frealqty' },
        { label: this.$t('market.xxprice'), prop: 'ftaxprice' },
        { label: this.$t('market.salerAmount'), prop: 'fallamount' },
        { label: this.$t('market.fsettlecurr'), prop: 'fsettlecurr' },
        { label: this.$t('market.fexchangerate_xs'), prop: 'fexchangerate_xs' },
        { label: this.$t('market.camatcost'), prop: 'ca_price' },
        { label: this.$t('market.costofAmount'), prop: 'ca_count' },
        { label: this.$t('market.localCurr'), prop: 'flocalcurr' },
        { label: this.$t('market.matercost'), prop: 'ca_stuffcost' },
        { label: this.$t('market.prdcost'), prop: 'ca_prdcost' },
        { label: this.$t('market.caPrdcost'), prop: 'ca_subcost' },
        { label: this.$t('market.makecost'), prop: 'ca_makecost' },
        { label: this.$t('market.paezText1'), prop: 'sal_khddh' },
        { label: this.$t('market.fsaleorderno'), prop: 'fsaleorderno' },
        { label: this.$t('market.cust'), prop: 'fcustname' },
        { label: this.$t('market.fcusttypename'), prop: 'fcusttypename' },
        { label: this.$t('market.saler'), prop: 'fsalesmanname' },
        { label: this.$t('market.stock2'), prop: 'fstockname' },
        { label: this.$t('market.creator'), prop: 'fcreator' },
        { label: this.$t('market.createtime'), prop: 'fcreatedate' },
        { label: this.$t('market.fdescripTion'), prop: 'fdescription', width: '200' }
      ],
      tableHeader1: []
    }
  },
  created() {
    // this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 230px)' : '73vh'
  },
  methods: {
    // 向下展
    load(row, treeNode, resolve) {
      const DATA = {
        fsaleorderentryid: row.fsaleorderentryid,
        fmaterialid: row.fmaterialid,
        frealqty: row.frealqty
      }
      querySalDedailCostList(DATA).then(res => {
        if (res.code === 0) {
          res.data.map(ite => {
            if (ite.below === 1) {
              ite.hasChildren = true
            }
            ite.ca_price = Number(ite.ca_price).toFixed(2)
            ite.ca_count = Number(ite.ca_count).toFixed(2)
            ite.ca_stuffcost = Number(ite.ca_stuffcost).toFixed(2)
            ite.ca_prdcost = Number(ite.ca_prdcost).toFixed(2)
            ite.ca_subcost = Number(ite.ca_subcost).toFixed(2)
            ite.ca_makecost = Number(ite.ca_makecost).toFixed(2)
          })
          setTimeout(() => {
            resolve(res.data)
          }, 1000)
        }
      })
    },
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          exportSalOrderCostList({
            pageSize: 100000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '销售成本明细表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取财务报表数据
    async handleGetUntreated(ev) {
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        pageNum: this.currentPage,
        pageSize: this.size,
        search: this.searCollData.concat(this.details)
      }
      const { data: RES } = await querySalOrderCostList(DATA)
      this.tableData = RES.array
      this.tableData.map(res => {
        res.hasChildren = true
        res.ca_price = Number(res.ca_price).toFixed(2)
        res.ca_count = Number(res.ca_count).toFixed(2)
        res.ca_stuffcost = Number(res.ca_stuffcost).toFixed(2)
        res.ca_prdcost = Number(res.ca_prdcost).toFixed(2)
        res.ca_subcost = Number(res.ca_subcost).toFixed(2)
        res.ca_makecost = Number(res.ca_makecost).toFixed(2)
      })
      this.total = RES.count
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.inventoryPosting {
  @include listBom;
  padding: 0;
  .header{
    position:relative;
    .header-name{
      width: 100%;
      span {
        width: 100px;
        font-size: 12px;
        text-align: center;
      }
    }
    .btn{
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
</style>
